package com.leo.jdkcore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

public class Core {
	@Test
	public void testResume(){
		String str1 = "hello";
		String str2 = "he" + new String("llo");
		System.out.println(str1==str2);
	}
	
	@Test
	public void coreString(){
		String s = "";
		int[] arr1 = {1,4,5,2,7,1,3};
		int[] arr2 = new int[3];
		//测试String类中调用的系统数组赋值方法
		System.arraycopy(arr1, 1, arr2, 0, 3);
		System.out.println(Arrays.toString(arr2));
	}
	
	@Test
	public void coreInteger(){
		//定义一个Integer 变量
		Integer i = 1;
		Integer j = 1;
		System.out.println(i==j); //true
		
		Integer x = 128;
		Integer y = 128;
		System.out.println(x==y); //false
		
		// 2147483647
		//System.out.println(Integer.MAX_VALUE);
		// Integer.TYPE
		//System.out.println(Integer.TYPE);
		
		System.out.println(Integer.reverse(2));
		System.out.println(Integer.highestOneBit(16));
	}
	
	@Test
	public void coreLong(){
		//定义方法内部类
		class LongCacheTest {
			Long cache[] = new Long[-(-128) + 127 + 1];
			
	        public LongCacheTest(){
	        	for(int i = 0; i < cache.length; i++)
	                cache[i] = new Long(i - 128);
	        }
	    }
		
		System.out.println(Arrays.toString(new LongCacheTest().cache));
		System.out.println(-(-128) + 127 + 1);
	}
	
	@Test
	public void coreArrayList1(){
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("abd");
		arrayList.add("dfg");
		arrayList.add("cvb");
		arrayList.add("wer");
		arrayList.add("asd");
		ArrayList<String> delArrayList = new ArrayList<String>();
		delArrayList.add("cvb");
		delArrayList.add("dfg");
		System.out.println("---------- remove all ----------");
		System.out.println(arrayList.removeAll(delArrayList)); //true
		System.out.println(arrayList); //[abd, wer, asd]
		System.out.println(delArrayList); //[cvb, dfg]
//		System.out.println("---------- retain all ----------");
//		System.out.println(arrayList.retainAll(delArrayList)); //true
//		System.out.println(arrayList); //[dfg, cvb]
//		System.out.println(delArrayList); //[cvb, dfg]
	}
	
	@Test
	public void coreArrayList2(){
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("abd");
		arrayList.add("dfg");
		arrayList.add("cvb");
		arrayList.add("wer");
		arrayList.add("asd");
		arrayList.add("kim");
		arrayList.add("pol");
		List<String> subList = arrayList.subList(2, 5);
		System.out.println("------------------ (一) -------------------");
		//arrayList: [abd, dfg, cvb, wer, asd, kim, pol]
		System.out.println("arrayList: "+arrayList);
		//subList: [cvb, wer, asd]
		System.out.println("subList: "+subList);
		subList.add("ooxx");
		System.out.println("------------------ (二) -------------------");
		//arrayList: [abd, dfg, cvb, wer, asd, ooxx, kim, pol]
		System.out.println("arrayList: "+arrayList);
		//subList: [cvb, wer, asd, ooxx]
		System.out.println("subList: "+subList);
		
	}
	
	@Test
	public void coreArrayList3(){
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("abd");
		arrayList.add("dfg");
		arrayList.add("cvb");
		arrayList.add("wer");
		arrayList.add("pol");
		Iterator<String> it = arrayList.iterator();
		System.out.println("arrayList: "+arrayList);
		while(it.hasNext()){
			String tmp = it.next();
			if(tmp.equals("dfg")){
				arrayList.remove(tmp);
				//it.remove();
			}
		}
		System.out.println("arrayList: "+arrayList);
	}
	
	@Test
	public void coreArrayList4(){
		CopyOnWriteArrayList<String> cwList = new CopyOnWriteArrayList<String>();
		cwList.add("abd");
		cwList.add("dfg");
		cwList.add("cvb");
		cwList.add("wer");
		cwList.add("asd");
		Iterator<String> it = cwList.iterator();
		System.out.println("arrayList: "+cwList);
		while(it.hasNext()){
			String tmp = it.next();
			if(tmp.equals("dfg")){
				cwList.remove(tmp);
				//it.remove();
			}
		}
		System.out.println("arrayList: "+cwList);
	}
	
	@Test
	public void coreConcurrentLinkedQueue(){
		ConcurrentLinkedQueue<String> queue1 = new ConcurrentLinkedQueue<String>();
		LinkedBlockingQueue<String> queue2 = new LinkedBlockingQueue<String>();
		ArrayBlockingQueue<String> queue3 = new ArrayBlockingQueue<String>(10);
	}
	
	@Test
	public void coreLinkedList(){
		/**
		 * @author Administrator
		 * @param <E>
		 * 节点类
		 */
		class MyNode<E> {
	        E item; //数据域
	        MyNode<E> next;  //后置指针
	        MyNode<E> prev;  //前置指针
	        // 构造方法接受 ：前置元素，数据域，后置元素
			MyNode(MyNode<E> prev, E element, MyNode<E> next) {
	            this.item = element;
	            this.next = next;
	            this.prev = prev;
	        }
	    }
		
		/* ********************		test		******************* */
		LinkedList<String> linkedList = new LinkedList<String>();
	}
	
	@Test
	public void coreMap(){
		HashMap<String,Object> map1 = new HashMap<String,Object>();
		HashSet<String> set = new HashSet<String>();
		TreeSet<String> set2 = new TreeSet<String>();
	}
	
	@Test
	public void coreAtomicInteger(){
		AtomicInteger ai = new AtomicInteger();
	}
	
	@Test
	public void coreStringBuilder(){
		StringBuilder sb1 = new StringBuilder();
		StringBuffer sb2 = new StringBuffer();
	}
}
